<?php
class Supports_Admin extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('utility_model');
        if(!modules::run('auth/auth/has_permission', array('operation' => OPERATION_MANAGE))) redirect('/');
    }

    function dispatcher($method='', $para1=NULL, $para2=NULL)
    {
        $layout             = 'powercms/admin_layout';
        $lang               = switch_language($para1);
        switch ($method)
        {
            case 'list_supports':
                $this->phpsession->save('current_support_lang', $lang);
                $main_content           = $this->list_supports(array('lang' => $lang));
                break;
            case 'add_support' :
                $main_content           = $this->add_support();
                break;
            case 'edit_support' :
                $main_content           = $this->edit_support();
                break;
        }

        $view_data                  = array();
        $view_data['admin_menu']    = modules::run('menus/menus/get_dashboard_menus');
        $view_data['main_content']  = $main_content;
        // META data
        $view_data['title']         = $this->_title;
        $this->load->view($layout, $view_data);
    }

    function list_supports($options = array())
    {
        $view_data = array();
        $view_data['supports']                  = $this->supports_model->get_supports($options);
        $this->_title                           = 'Quản lý hỗ trợ trực tuyến' . DEFAULT_TITLE_SUFFIX;
        $view_data['language_combobox']         = $this->utility_model->get_lang_combo(array('lang' => $options['lang'], 'extra' => 'onchange="javascript:change_lang();"'));
        $view_data['back_url']                  = '/dashboard/supports/';
        return $this->load->view('admin/list_supports',$view_data, TRUE);
    }

    public function add_support()
    {
        $options    = array();
        $view_data  = array();
        $lang = $this->phpsession->get('current_support_lang');
        
        if($this->is_postback())
        {
            if (!$this->_do_add_support())
                $options['error'] = validation_errors();
        }
        $view_data = $this->_get_add_support_form_data();
        if (isset($options['error'])) $view_data['options']   = $options;

        $this->_title   = 'Thêm hỗ trợ trực tuyến' . DEFAULT_TITLE_SUFFIX;
        $view_data['language_combobox']         = $this->utility_model->get_lang_combo(array( 'combo_name' => 'language', 'language' => $lang ));
        $view_data['back_url']                  = '/dashboard/supports/';
        
        return $this->load->view('admin/add_support_form', $view_data, TRUE);
    }

    /**
     * Chuẩn bị dữ liệu cho form add
     * @return type
     */
    private function _get_add_support_form_data()
    {
        $view_data                          = array();
        $view_data['title']         = $this->input->post('title');
        $view_data['type']          = $this->input->post('type');
        $view_data['content']       = $this->input->post('content');
        $view_data['lang']          = $this->input->post('language');
        $view_data['header']        = 'Thêm hỗ trợ trực tuyến';
        $view_data['button_name']   = 'Thêm';
        $view_data['submit_uri']    = '/dashboard/supports/add';
        return $view_data;
    }

    private function _do_add_support()
    {
        $this->form_validation->set_rules('content', 'Tài khoản hỗ trợ', 'trim|required|xss_clean');

        if ($this->form_validation->run())
        {
            $post_data = $this->_get_posted_support_data();

            $supports = $this->supports_model->get_supports(array('last_row' => TRUE, 'lang' => $this->phpsession->get('current_support_lang')));
            $post_data['position'] = is_object($supports) ? $supports->position + 1 : 1;
            $this->supports_model->add_support($post_data);

            redirect('/dashboard/supports/' . $this->phpsession->get('current_support_lang'));
        }
        return FALSE;
    }

    private function _get_posted_support_data()
    {
        $post_data = array(
            'title'             => my_trim($this->input->post('title', TRUE)),
            'type'              => $this->input->post('type', TRUE),
            'content'           => my_trim($this->input->post('content', TRUE)),
            'lang'              => my_trim($this->input->post('language', TRUE)),
        );
        return $post_data;
    }

    public function edit_support()
    {
        if(!$this->is_postback()) redirect('/dashboard/supports');

        $view_data  = array();
        $options    = array();
        if ($this->is_postback() && !$this->input->post('from_list'))
        {
            if (!$this->_do_edit_support())
                $options['error'] = validation_errors();
        }
        $view_data = $this->_get_edit_form_data();
        if (isset($options['error'])) $view_data['options'] = $options;
        $this->_title   = 'Sửa thông tin hỗ trợ trực tuyến' . DEFAULT_TITLE_SUFFIX;
        $view_data['language_combobox'] = $this->utility_model->get_lang_combo(array('combo_name' => 'language', 'language' => $view_data['lang']));
        return $this->load->view('admin/add_support_form', $view_data, TRUE);
    }
    /**
     * Chuẩn bị dữ liệu cho form sửa
     * @return type
     */
    private function _get_edit_form_data()
    {

        // khi vừa vào trang sửa
        if($this->input->post('from_list'))
        {
            $support_id         = $this->input->post('support_id');
            $support            = $this->supports_model->get_supports(array('id' => $support_id));
            $id                 = $support->id;
            $title              = $support->title;
            $type               = $support->type;
            $content            = $support->content;
            $lang               = $support->lang;
        }

        else
        {
            $id             = $this->input->post('support_id');
            $title          = my_trim($this->input->post('title', TRUE));
            $type           = $this->input->post('type');
            $content        = my_trim($this->input->post('content', TRUE));
            $lang           = my_trim($this->input->post('language', TRUE));
        }

        $view_data                          = array();
        $view_data['id']                    = $id;
        $view_data['title']                 = $title;
        $view_data['type']                  = $type;
        $view_data['content']               = $content;
        $view_data['lang']                  = $lang;

        $view_data['header']                = 'SỬA HỖ TRỢ TRỰC TUYẾN';
        $view_data['button_name']           = 'Sửa';
        $view_data['submit_uri']            = '/dashboard/supports/edit';
        return $view_data;
    }
    /**
     *  sửa trong DB nếu Validate OK
     * @return type
     */
    private function _do_edit_support()
    {
        $this->form_validation->set_rules('content', 'Tài khoản hỗ trợ', 'trim|required|xss_clean');

        if ($this->form_validation->run())
        {
            $post_data = $this->_get_posted_support_data();
            $post_data['id'] = $this->input->post('support_id');
            $this->supports_model->update_support($post_data);

            redirect('/dashboard/supports/' . $this->phpsession->get('current_support_lang'));
        }
        return FALSE;
    }

    /**
     * Xóa tin
     */
    public function delete_support()
    {
        if($this->is_postback())
        {
            $support_id = $this->input->post('support_id');
            $this->supports_model->delete_support($support_id);
        }
        redirect('/dashboard/supports/' . $this->phpsession->get('current_support_lang'));
    }

    public function sort_support()
    {
        $arr = $this->input->post('id');
        $i = 1;
        foreach ($arr as $recordidval)
        {
            $array = array('position' => $i);
            $this->db->where('id', $recordidval)
                     ->where('lang', $this->phpsession->get('current_support_lang'))
                     ->update('supports', $array);
            $i = $i + 1;
        }
    }
}
?>
